package com.example.ItPhotoAlbumCloud.utils;


import org.slf4j.Logger;

/**
 * 日志工具
 *  小仙 创建人 2023-03-12 08:36:32.
 */
public class LoggerUtils {

    public static void info(Logger logger, String message){
        logger.info(message  + trace());
    }

    public static void info(Logger logger, String message, Object... param){
        message = setMessage(message, param);
        logger.info(message);
    }


    public static void warn(Logger logger, String message){
        logger.warn(message  + trace());
    }

    public static void warn(Logger logger, String message, Object... param){
        message = setMessage(message, param);
        logger.warn(message);
    }

    public static void error(Throwable e,Logger logger, String message){
        logger.error(message + trace() , e);
    }

    public static void error(Throwable e,Logger logger, String message, Object... param){
        message = setMessage(message, param);
        logger.error(message, e);
    }

    private static String setMessage(String message, Object... param){

        if (null == param){
            return message;
        }

        for (int i = 0; i < param.length; i++) {
            String replace = "{" + i + "}";
            if (!message.contains(replace)){
                continue;
            }
            String replaceN = "" + param[i];
            message = message.replace(replace, replaceN);
        }

        return message + trace();
    }

    private static String trace(){
        return "-traceId:" + CommonTemplate.getTraceId();
    }
}